package com.zelin.shop.web;


import com.zelin.utils.FastDFSClient;
import com.zelin.utils.R;
import org.csource.fastdfs.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * Created by WF on 2021/4/8 21:16
 */
@RestController
public class UploadController {
    /**
     * 功能: 测试原生的文件上传
     * 参数:
     * 返回值: void
     * 时间: 2021/7/28 15:06
     */
    @RequestMapping("uploadxxx")
    public void uploadxxx() throws Exception {
        // //1.对当前fsastdsf的环境进行初始化处理
        ClientGlobal.init("E:\\idea-projects\\java0426\\zyg-parent\\zyg-manager-web\\src\\main\\resources\\fastdfs.conf");
        //2.定义一个TrackerCleint对象
        TrackerClient trackerClient = new TrackerClient();
        //3.根据trackerClient得到trackerServer对象
        TrackerServer trackerServer = trackerClient.getConnection();
        //4.定义一个StorageServer
        StorageServer storageServer = null;
        //5.构造一个StorageClient对象
        StorageClient storageClient = new StorageClient(trackerServer,storageServer);
        //6.进行文件上传
        String[] strings = storageClient.upload_file("d:/abc.jpg", "jpg", null);
        //7.打印得到上传文件每一部分的值
        for (String string : strings) {
            System.out.println(string);
        }
    }
    @RequestMapping("/upload")
    public R upload(MultipartFile file) throws Exception {
        //1.得到fastDFSClient对象
        FastDFSClient fastDFSClient = new FastDFSClient("classpath:fastdfs.conf");
        //2. 获取文件名
        String filename = file.getOriginalFilename();
        //3. 得到文件后缀名
        String suffixName = filename.substring(filename.lastIndexOf(".") + 1);
        System.out.println("filename = " + filename);
        //4. 上传文件
        String s = fastDFSClient.uploadFile(file.getBytes(), suffixName);
        //5. 最后的地址
        String url = "http://192.168.56.10:8080/" + s;

        return R.ok().put("url",url);
    }
}
